import matplotlib.pyplot as plt

# 数据
years = ['2022', '2023']
revenue = [1231.71, 1261.79]  # 营业总收入，单位：亿元

# 创建一个图形对象和一个轴对象
fig, ax = plt.subplots(figsize=(10, 6))

# 绘制柱状图
bars = ax.bar(years, revenue, color='skyblue', label='Operating Revenue (Billion)')

# 添加数值标签到柱状图上
for bar, yval in zip(bars, revenue):
    ax.text(bar.get_x() + bar.get_width()/2 - 0.1, yval + 5, f'{yval:.2f}B', ha='center', va='bottom', fontsize=12, color='black', fontweight='bold')

# 添加标题和标签
ax.set_title('Operating Revenue Growth Comparison of 2022 and 2023', fontsize=16, fontweight='bold')
ax.set_xlabel('Year', fontsize=14)
ax.set_ylabel('Operating Revenue (Billion)', fontsize=14)

# 设置y轴的范围以适应数据，并留出一些空间
ax.set_ylim(bottom=min(revenue) - 20, top=max(revenue) + 20)

# 添加图例
ax.legend(loc='upper left', bbox_to_anchor=(0.1, 0.9), fontsize=12)

# 美化图形
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.yaxis.grid(True, linestyle='--', linewidth=0.7, color='gray')

# 计算增长率并添加到图表中
growth_rate = f'({(revenue[1] - revenue[0]) / revenue[0] * 100:.2f}%)'
ax.annotate(growth_rate, xy=(0.5, 0.95), xycoords='axes fraction', fontsize=12, color='black', ha='center', va='top', bbox=dict(boxstyle="round", fc="w", ec="k", pad=0.3))

# 显示图形
plt.tight_layout()
plt.savefig('operating_revenue_growth_comparison_2022_2023_corrected.png')  # 保存图片
plt.show()